import {directionEnum} from "../directionEnum";
import config from '../config';
import audio from "../service/audio"
export default abstract class modelAbstract{
    abstract name:string;
    abstract canvas:ICanvas;
    abstract render():void;
    abstract image():HTMLImageElement;
    public width = config.model.width;
    public height = config.model.height;
    public direction:directionEnum = directionEnum.bottom;
    constructor(public x:number,public y:number){
        this.randomDirection()
    }
    
    // 随机方向
    protected randomDirection(){
        this.direction =  Object.keys(directionEnum)[Math.floor(Math.random() * 4)] as directionEnum;
    }

    protected draw(){
        this.canvas.ctx.drawImage(this.image(),this.x,this.y,config.model.width,config.model.height);
    }

    public destroy(){
        this.canvas.removeModel(this);
        this.canvas.renderModels();
    }

    protected blast(model:IModel){
        audio.blast()
        Array(...Array(8).keys()).reduce((promise,value) => {
            return new Promise((resolve) => {
                setTimeout(() => {
                    const img = new Image();
                    img.src = `/src/static/images/blasts/blast${value}.gif`;
                    img.onload = () => {
                        this.canvas.ctx.drawImage(img,model.x,model.y,model.width,model.height);
                        resolve(promise);
                    }
                },100)
            })
        },Promise.resolve())
    }
}